package com.whu.waterworks.impl;

import java.util.List;

import org.springframework.orm.hibernate4.HibernateTemplate;

import com.whu.waterworks.dao.HistorydataDAO;
import com.whu.waterworks.model.Historydata;
import com.whu.waterworks.model.HistorydataId;
import com.whu.waterworks.model.Realtimedata;
/**
 * 
 * @author wangying
 *
 */
public class HistorydateDAOImpl implements HistorydataDAO{

private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	@Override
	public boolean save(Historydata entity) {
		// TODO Auto-generated method stub
		try {
			hibernateTemplate.save(entity);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean delete(Historydata entity) {
		// TODO Auto-generated method stub
		try {
			hibernateTemplate.delete(entity);
			
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		return true;
		
	}

	@Override
	public boolean update(Historydata entity) {
		// TODO Auto-generated method stub
		try {
			hibernateTemplate.update(entity);
			
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		return true;
	}

	@Override
	public Historydata findById(HistorydataId id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Historydata> findByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Historydata> findByValue(Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Historydata> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Historydata> findByIdAll(String id, String type) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Historydata> historydatas = (List<Historydata>)hibernateTemplate.find("from Historydata rd where rd.id.stationnum='"+id+"' and rd.id.type like '%"+type+"%' order by rd.id.time desc");
		if(historydatas.size()==0)
			return null;
		return historydatas;
	}

	@Override
	public List<Historydata> findByIdAllInTime(String id, String type,
			String endTime, String beginTime) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Historydata> historydatas = (List<Historydata>)hibernateTemplate.find("from Historydata rd where rd.id.stationnum='"+id+"' and rd.id.type like '%"+type
				+"%' and rd.id.time>='"+beginTime+"' and rd.id.time<='"+endTime +
				"' order by rd.id.time desc");
		if(historydatas.size()==0)
			return null;
		return historydatas;
	}
	
	@Override
	public List<Historydata> findByIdAllTime(String id, String type,
			 String beginTime) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Historydata> historydatas = (List<Historydata>)hibernateTemplate.find("from Historydata rd where rd.id.stationnum='"+id+"' and rd.id.type like '%"+type
				+"%' and rd.id.time>='"+beginTime+ 
				"' order by rd.id.time desc");
		if(historydatas.size()==0)
			return null;
		return historydatas;
	}

	
	
}
